package org.tote.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.tote.dao.AuditDao;
import org.tote.model.Audit;
import org.tote.model.AuditOperationType;

@Service
@Transactional
public class AuditServiceImpl implements AuditService {
	
	@Autowired
	private AuditDao auditDao;

	
	@Override
	public void saveAudit(Audit audit){
		Assert.notNull(audit, "audit is null");
		auditDao.save(audit);
	}
	
	@Override
	public List<String> getAuditOperationTypes() {
		List<String> auditionOperationTypes = new ArrayList<String>();
		for (AuditOperationType t : AuditOperationType.values()){
			auditionOperationTypes.add(t.toString());
		}
		return auditionOperationTypes;
	}

	@Override
	public List<Audit> getAuditRecords(AuditOperationType type, Date beginDate, Date endDate, String userName, int page, int limit) {
		return auditDao.getAuditRecords(type, beginDate, endDate, userName, page, limit);
	}

	@Override
	public long getMaxPage(AuditOperationType type, Date beginDate, Date endDate, String userName, int limit) {
		long size = auditDao.getAuditCount(type, beginDate, endDate, userName);
		if (size == 0){
			return 0;
		}
		return (size - 1) / limit;
	}
}
